package com.ruoyi.kucunpandian.controller;

import java.util.List;

import com.ruoyi.shangpinxinxi.domain.TestResult;
import com.ruoyi.shangpinxinxi.domain.Shangpinxinxi;
import com.ruoyi.shangpinxinxi.service.IShangpinxinxiService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.kucunpandian.domain.Kucunpandian;
import com.ruoyi.kucunpandian.service.IKucunpandianService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 库存盘点Controller
 * 
 * @author ruoyi
 * @date 2023-05-17
 */
@Controller
@RequestMapping("/kucunpandian/kucunpandianTest")
public class KucunpandianController extends BaseController
{
    private String prefix = "kucunpandian/kucunpandianTest";

    @Autowired
    private IKucunpandianService kucunpandianService;

    @Autowired
    private IShangpinxinxiService shangpinxinxiService;

    @RequiresPermissions("kucunpandian:kucunpandianTest:view")
    @GetMapping()
    public String kucunpandianTest()
    {
        return prefix + "/kucunpandianTest";
    }

    /**
     * 查询库存盘点列表
     */
    @RequiresPermissions("kucunpandian:kucunpandianTest:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Kucunpandian kucunpandian)
    {
        startPage();
        List<Kucunpandian> list = kucunpandianService.selectKucunpandianList(kucunpandian);
        return getDataTable(list);
    }

    /**
     * 导出库存盘点列表
     */
    @RequiresPermissions("kucunpandian:kucunpandianTest:export")
    @Log(title = "库存盘点", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Kucunpandian kucunpandian)
    {
        List<Kucunpandian> list = kucunpandianService.selectKucunpandianList(kucunpandian);
        ExcelUtil<Kucunpandian> util = new ExcelUtil<Kucunpandian>(Kucunpandian.class);
        return util.exportExcel(list, "库存盘点数据");
    }

    /**
     * 新增库存盘点
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存库存盘点
     */
    @RequiresPermissions("kucunpandian:kucunpandianTest:add")
    @Log(title = "库存盘点", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Kucunpandian kucunpandian)
    {
        TestResult testResult = new TestResult();
        testResult.setMenName(kucunpandian.getGoodName());
        testResult.setLeibie(kucunpandian.getShangpinLeibie());
        testResult.setMenKuan(kucunpandian.getShangpinGuigeKuan());
        testResult.setMenGao(kucunpandian.getShangpinGuigeGao());
        testResult.setKaiXiang(kucunpandian.getShangpinKaixiang());
        if (kucunpandian.getGoodName().contains("防火锁")
                || kucunpandian.getGoodName().contains("闭门器")
                || kucunpandian.getGoodName().contains("拉爆盖")) {
            testResult.setMenKuan(null);
            testResult.setMenGao(null);
            testResult.setKaiXiang(null);
        }
        List<Shangpinxinxi> shangpinxinxi = shangpinxinxiService.selectShangpinxinxiByNameAndSize2(testResult);
        if (shangpinxinxi.size() == 0) {
            return toAjax("商品信息中尚无该商品");
        }

        kucunpandian.setShangpinTuPian(shangpinxinxi.get(0).getTiaoma());
        return toAjax(kucunpandianService.insertKucunpandian(kucunpandian));
    }

    /**
     * 修改库存盘点
     */
    @RequiresPermissions("kucunpandian:kucunpandianTest:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Kucunpandian kucunpandian = kucunpandianService.selectKucunpandianById(id);
        mmap.put("kucunpandian", kucunpandian);
        return prefix + "/edit";
    }

    /**
     * 修改保存库存盘点
     */
    @RequiresPermissions("kucunpandian:kucunpandianTest:edit")
    @Log(title = "库存盘点", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Kucunpandian kucunpandian)
    {
        return toAjax(kucunpandianService.updateKucunpandian(kucunpandian));
    }

    /**
     * 删除库存盘点
     */
    @RequiresPermissions("kucunpandian:kucunpandianTest:remove")
    @Log(title = "库存盘点", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(kucunpandianService.deleteKucunpandianByIds(ids));
    }
}
